package org.dromara.datacheck.mytest;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 刘武贵
 * @Date 2024/9/17 15:57
 * @Description 功能描述：返回日期汇总的数据结构测试
 */
public class SalesOrderData {

    public static void main(String[] args) {
        // 示例数据
        List<DataEntry> dataEntries = new ArrayList<>();
        dataEntries.add(new DataEntry("金蝶", 10, 20, 30, 40, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0));
        dataEntries.add(new DataEntry("聚水潭", 60, 70, 80, 90, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0));
        dataEntries.add(new DataEntry("差异", 50, 50, 50, 50, -40, 0, 0, 0, 0, 0, 0, 0, 0, -1));

        // 组织数据
        Map<String, Integer> sumsJdMap = new LinkedHashMap<>();
        Map<String, Integer> sumsJstMap = new LinkedHashMap<>();
        Map<String, Integer> differences = new LinkedHashMap<>();
        List<String> labels = new ArrayList<>();

        // 生成日期列表
        for (int i = 1; i <= 15; i++) {
            String date = "8月" + (i < 10 ? "0" : "") + i + "日";
            labels.add(date);
        }

        // 计算每个日期的数量之和和差异
        for (int i = 0; i < labels.size(); i++) {
            String date = labels.get(i);
            int sumsJd = dataEntries.get(0).getData(i + 1);
            int sumsJst = dataEntries.get(1).getData(i + 1);
            int diff = dataEntries.get(2).getData(i + 1);

            sumsJdMap.put(date, sumsJd);
            sumsJstMap.put(date, sumsJst);
            differences.put(date, diff);
        }

        // 构建JSON
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("labels", labels);
        result.put("sumsJd", sumsJdMap);
        result.put("sumsJst", sumsJstMap);
        result.put("differences", differences);

        // 转换为JSON字符串
        Gson gson = new Gson();
        String jsonResult = gson.toJson(result);
        System.out.println(jsonResult);
    }

    static class DataEntry {
        private String label;
        private int[] data = new int[15];

        public DataEntry(String label, int... data) {
            this.label = label;
            System.arraycopy(data, 0, this.data, 0, data.length);
        }

        public int getData(int index) {
            return data[index - 1];
        }
    }
}
